//
//  BaseLevel1Controller.m
//  Stock
//
//  Created by blacksun on 11-7-4.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "BaseLevel1Controller.h"
#import "QuotationCenterController.h"

@implementation BaseLevel1Controller
@synthesize tableView;
@synthesize type;
@synthesize requestUrl;
@synthesize quotationCenterService;
@synthesize dataDicts;



#pragma mark -Service Delegate
-(void)requestFinishedWithResult:(id)aResult andUserinfo:(NSDictionary *)aUserinfo{
    self.dataDicts = aResult;
    self.tableView.tableFooterView = nil;
    [self.tableView reloadData];
    
}
-(void)requestFinishedwithError:(NSError *)aError andUserinfo:(NSDictionary *)aUserinfo{
    
}

- (void)dealloc
{
    self.tableView = nil;
    self.type = nil;
    self.requestUrl = nil;
    [quotationCenterService cancleAllRequest];
    self.quotationCenterService = nil;
    self.dataDicts = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.dataDicts = [NSArray array];
    self.quotationCenterService = [[[QuotationCenter alloc] initWithDelegate:self] autorelease];
    [quotationCenterService queryLevel1DataWithUri:requestUrl];
    self.tableView.tableFooterView = [[StockAppHelper shareInstance] getTableFooterView];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    self.tableView = nil;
    [quotationCenterService cancleAllRequest];
    self.quotationCenterService = nil;
    self.dataDicts = nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [dataDicts count];
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"BaseLevel1Controller";
    
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIColor *clearColor = [UIColor clearColor];
        UIColor *whiteColor = [UIColor whiteColor];
        
        cell.textLabel.backgroundColor = clearColor;
        cell.textLabel.textColor = whiteColor;
        cell.textLabel.font = [UIFont systemFontOfSize:17.0];
        
        UILabel *avgLabel = [[UILabel alloc] initWithFrame:CGRectMake(120.0, 12.0, 80.0, 20.0)];
        avgLabel.backgroundColor = clearColor;
        avgLabel.textColor = whiteColor;
        avgLabel.tag = kAvgChangedRatioLabelTag;
        avgLabel.textAlignment = UITextAlignmentRight;
        
        UILabel *fNetLabel = [[UILabel alloc] initWithFrame:CGRectMake(210.0, 12.0, 100.0, 20.0)];
        fNetLabel.textColor = whiteColor;
        fNetLabel.backgroundColor = clearColor;
        fNetLabel.tag = kFNetLabelTag;
        fNetLabel.textAlignment = UITextAlignmentRight;
        
        [cell.contentView addSubview:avgLabel];
        [cell.contentView addSubview:fNetLabel];
        
        [avgLabel release];
        [fNetLabel release];
        
    }
    
    UILabel *avgLabel = (UILabel *)[cell viewWithTag:kAvgChangedRatioLabelTag];
    UILabel *fNetLabel = (UILabel *)[cell viewWithTag:kFNetLabelTag];
    
    NSDictionary *dict = [dataDicts objectAtIndex:indexPath.row];
    if ([[dict objectForKey:kFNet] hasPrefix:@"-"]) {
        fNetLabel.textColor = [UIColor greenColor];
    }else{
        fNetLabel.textColor = [UIColor redColor];
    }
    if ([[dict objectForKey:kAvgChangedRatio] hasPrefix:@"-"]) {
        avgLabel.textColor = [UIColor greenColor];
    }else{
        avgLabel.textColor = [UIColor redColor];
    }
    
    cell.textLabel.text = [dict objectForKey:kName];
    avgLabel.text = [dict objectForKey:kAvgChangedRatio];
    fNetLabel.text = [dict objectForKey:kFNet];
    
    NSString *imageName = @"cellbg2.png";
    if (indexPath.row%2==1) {
        imageName = @"cellbg1.png";
    }
    cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]] autorelease];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BaseQuotationStockListController *childController = nil;
    if ([type isEqualToString:kIndustryTypeKey]) {
        childController = [[IndustryStockListController alloc] initWithNibName:@"BaseQuotationStockListController" bundle:nil];
    }else if([type isEqualToString:kConceptTypeKey]){
        childController = [[ConceptStockListController alloc] initWithNibName:@"BaseQuotationStockListController" bundle:nil];
    }
    childController.key = [[dataDicts objectAtIndex:indexPath.row] objectForKey:kUUID];
    childController.title = [[dataDicts objectAtIndex:indexPath.row] objectForKey:kName];
    [self.navigationController pushViewController:childController animated:YES];
    [childController release];
}


@end